package jdk7;

import java.util.Random;

/**
 * @author honway.liu
 * @date 2018/4/30 下午9:56
 * @email gm100861@gmail.com
 * @blog http://linuxsogood.org
 */
public class Hare implements Runnable{

    private int totalStep;

    private int step;

    private static final Random random = new Random();

    public Hare(int totalStep) {
        this.totalStep = totalStep;
    }

    @Override
    public void run() {
        while (step < totalStep) {
            try {
                Thread.sleep(1000);
                if (random.nextInt() % 2 == 0) {
                    Thread.sleep(2000);
                    System.out.println("兔子睡了2秒 ZZZ");
                } else {
                    step+=2;
                    System.out.println("兔子跑了:[" + step + "]步.");
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
